package com.carkeeper.role.service.impl;

import com.carkeeper.menu.dao.MenuMapper;
import com.carkeeper.pojo.Department;
import com.carkeeper.pojo.Menu;
import com.carkeeper.pojo.Permission;
import com.carkeeper.pojo.Role;
import com.carkeeper.role.dao.PermissionMapper;
import com.carkeeper.role.dao.RoleMapper;
import com.carkeeper.role.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> findAll() {

        List<Role> roleList = roleMapper.selectAll();
        return roleList;
    }

    @Override
    public Page<Role> findPage(int page, int size) {

        PageHelper.startPage(page,size);
        return (Page<Role>) roleMapper.selectAll();
    }

    @Override
    public Page<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Role>)roleMapper.selectByExample(example);
    }

    @Override
    public int addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {

        Role selectOne = roleMapper.selectOne(role);
        //判断角色是否存在
        if (selectOne != null){
            throw new RuntimeException("该角色不存在");
        }
        // 角色存在，先删除角色权限中间表的数据
        int i = permissionMapper.deletePermissionByRoleId(role.getId());
        int i1 = menuMapper.deleteMenuListByRoleId(role.getId());
        if (i <= 0 || i1 <= 0) {
            throw new RuntimeException("删除角色对应的权限或者菜单失败");
        }
        // 为角色重新授权
        for (Integer permissionId : permissionIds) {
            permissionMapper.addPermissionByRoleId(role.getId(), permissionId);
        }
        // 为角色重新添加菜单
        for (Integer menuId : menuIds) {
            menuMapper.addMenuListByRoleIdAndMenuId(role.getId(), menuId);
        }
        return roleMapper.updateByPrimaryKeySelective(role);

    }

    @Override
    public int deleteById(Integer id) {
        // 查询角色权限中间表是否有数据
        Set<Permission> permissionList = permissionMapper.findPermissionByRoleId(id);
        if (permissionList == null || permissionList.size() <= 0) {
            // 提示信息
            throw new RuntimeException("该账户异常，未授予权限");
        }
        // 先遍历删除角色权限中间表数据
        permissionMapper.deletePermissionByRoleId(id);
        // 查询角色菜单表是否有数据
        LinkedHashSet<Menu> menuList = menuMapper.findMenuList2ByRoleId(id);
        if (menuList == null && menuList.size() <= 0) {
            throw new RuntimeException("该账户异常，未授予可操作的一级菜单菜单");
        }
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Role selectOne = roleMapper.selectOne(role);
        if (selectOne == null) {
            throw new RuntimeException("该角色不存在, 无法删除");
        }
        // 角色存在，先删除角色权限中间表的数据
        int i = permissionMapper.deletePermissionByRoleId(role.getId());
        int i1 = menuMapper.deleteMenuListByRoleId(role.getId());
        if (i <= 0 || i1 <= 0) {
            throw new RuntimeException("删除角色对应的权限或者菜单失败");
        }
        // 为角色重新授权
        for (Integer permissionId : permissionIds) {
            permissionMapper.addPermissionByRoleId(role.getId(), permissionId);
        }
        // 为角色重新添加菜单
        for (Integer menuId : menuIds) {
            menuMapper.addMenuListByRoleIdAndMenuId(role.getId(), menuId);
        }
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public Role findById(Integer id) {
        // 查找对应的角色
        Role role = roleMapper.selectByPrimaryKey(id);

        return role;
    }


    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 是否显示
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            // 是否导航
            if (searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu"))) {
                criteria.andLike("isMenu", "%" + searchMap.get("isMenu") + "%");
            }

            // 分类ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 商品数量
            if (searchMap.get("goodsNum") != null) {
                criteria.andEqualTo("goodsNum", searchMap.get("goodsNum"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
            // 上级ID
            if (searchMap.get("parentId") != null) {
                criteria.andEqualTo("parentId", searchMap.get("parentId"));
            }
            // 模板ID
            if (searchMap.get("templateId") != null) {
                criteria.andEqualTo("templateId", searchMap.get("templateId"));
            }

        }
        return example;
    }
}
